﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.DALFactory;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.BLL
{
    public sealed class MasEducationInstituteBLL : IMasEducationInstituteBLL
    {
        private static readonly IMasEducationInstituteDAL dal = DataAccess.CreateMasterEducationInstitute();

        private MasEducationDegreeInfo degreeInfo = new MasEducationDegreeInfo();
        private MasDistrictInfo districtInfo = new MasDistrictInfo();
        private MasCountryInfo countryInfo = new MasCountryInfo();
        private MasEducationInstituteInfo insInfo = new MasEducationInstituteInfo();

        public IList<MasEducationInstituteInfo> ListEducationInstitutes()
        {
            return dal.ListEducationInstitutes(new MasEducationInstituteInfo(0));
        }
        public IList<MasEducationInstituteInfo> ListEducationInstitutes(string Sequence)
        {
            int Seq = (Sequence.Length > 0 ? Int32.Parse(Sequence) : 0);
            return dal.ListEducationInstitutes(new MasEducationInstituteInfo(Seq));
        }
        public IList<MasEducationInstituteInfo> ListEducationInstitutesLocal(bool Local)
        {
            if (Local)
            {
                countryInfo.Sequence = 140;
            }
            else
                countryInfo.Sequence = 1;

            insInfo.Countries = countryInfo;
            return dal.ListEducationInstitutes(insInfo);
        }
        public DataSet LoadEducationInstitutes(Nullable<int> Sequence, bool Local, string SearchText)
        {
            int Seq = (Sequence.HasValue ? (int)Sequence : 0);
            if (Local)
            {
                countryInfo.Sequence = 140;
            }
            else
                countryInfo.Sequence = 1;

            insInfo.Sequence = Seq;
            insInfo.Countries = countryInfo;
            insInfo.strFilter = SearchText;

            return dal.LoadEducationInstitutes(insInfo);
        }
        public void Insert(string Code, string Name, string NameEng, bool Governments, string Address, string District
            , string Country, string PostalCode, string Tel, string Fax, string EducationDegree, int CreatedBy)
        {
            int BeGov = Governments ? 1 : 0;

            try
            {
                degreeInfo.Sequence = EducationDegree.Length > 0 ? Int32.Parse(EducationDegree) : 0;
                districtInfo.Sequence = District.Length > 0 ? Int32.Parse(District) : 0;
                countryInfo.Sequence = Country.Length > 0 ? Int32.Parse(Country) : 0;

                dal.Insert(new MasEducationInstituteInfo(0, Code, Name, NameEng, BeGov
                                , new AddressInfo(Address, PostalCode, Tel, Fax, "", "")
                                , districtInfo
                                , countryInfo
                                , degreeInfo, "", CreatedBy, 0)); 
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Update(int Sequence, string Code, string Name, string NameEng, bool Governments, string Address, string District
            , string Country, string PostalCode, string Tel, string Fax, string EducationDegree, int UpdatedBy)
        {
            int BeGov = Governments ? 1 : 0;

            try
            {
                degreeInfo.Sequence = EducationDegree.Length > 0 ? Int32.Parse(EducationDegree) : 0;
                districtInfo.Sequence = District.Length > 0 ? Int32.Parse(District) : 0;
                countryInfo.Sequence = Country.Length > 0 ? Int32.Parse(Country) : 0;

                dal.Update(new MasEducationInstituteInfo(Sequence, Code, Name, NameEng, BeGov
                                , new AddressInfo(Address, PostalCode, Tel, Fax, "", "")
                                , districtInfo
                                , countryInfo
                                , degreeInfo, "", 0, UpdatedBy));
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                dal.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
    }
}
